//
//  Reuseable.swift
//  ifanr
//
//  Created by 😱😱 on 16/9/13.
//  Copyright © 2016年 😱😱. All rights reserved.
//

import UIKit

public protocol Reuseable: class {
    static var reuseIdentifier: String { get }
}

extension Reuseable {
    static var reuseIdentifier : String {
        return String(Self)
    }
}

public extension UITableView {
    func dequeueReusableCell<T: Reuseable>() -> T? {
        return self.dequeueReusableCellWithIdentifier(T.reuseIdentifier) as! T?
    }
}

public extension UICollectionView {
    
    func dequeueReusableCell<T: Reuseable>(indexPath: NSIndexPath) -> T {
        return self.dequeueReusableCellWithReuseIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as! T
    }
    
    func registerClass<T: UICollectionViewCell where T: Reuseable>(_: T.Type) {
        self.registerClass(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
    
    func registerClass<T: UICollectionReusableView where T: Reuseable>(_: T.Type, forSupplementaryViewOfKind: String) {
        return self.registerClass(T.self, forSupplementaryViewOfKind: forSupplementaryViewOfKind, withReuseIdentifier: T.reuseIdentifier)
    }
    
    func dequeueReusableSupplementaryViewOfKind<T: UICollectionReusableView where T: Reuseable>(elementKind: String, indexPath: NSIndexPath) -> T {
        return self.dequeueReusableSupplementaryViewOfKind(elementKind, withReuseIdentifier: T.reuseIdentifier, forIndexPath: indexPath) as! T
    }
}
